package test.com.newproject.framework.image;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.TypedValue;
import android.widget.ImageView;

import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;

/**
 * 图片处理中心
 * <p>
 * Created by zjh on 2017/8/4.
 */
public class ImageCenter {

    public static final int TRANSFORM_TYPE_DEFAULT = 0x00;
    public static final int TRANSFORM_CENTER_CROP = 0x02;
    public static final int TRANSFORM_CENTER_INSIDE = 0x03;
    public static final int TRANSFORM_FIT_CENTER = 0x04;
    public static final int TRANSFORM_ROUNDED_CORNERS = 0x05;
    public static final int TRANSFORM_CENTER_CROP_ROUNDED_CORNERS = 0x06;
    public static final int TRANSFORM_CIRCLE_CROP = 0x07;
    public static final int TRANSFORM_CENTER_CROP_CIRCLE_CROP = 0x08;
    public static final int TRANSFORM_CUSTOM = 0x09;

    public static GlideRequests with(Context context) {
        return GlideApp.with(context);
    }

    public static void load(Context context, Object source, ImageView imageView) {
        GlideApp.with(context)
                .load(source)
                .into(imageView);
    }

    public static void load(Context context, Object source, ImageView imageView, RequestOptions options) {
        GlideApp.with(context)
                .load(source)
                .apply(options)
                .into(imageView);
    }

    public static void load(final Options options) {
        loadOptions(options);
    }

    public static void preload(final Options options) {
        options.pre = true;
        loadOptions(options);
    }

    public static void loadOptions(final Options options) {
        if (options == null) {
            return;
        }

        if (options.context == null) {
            return;
        }

        if (options.source == null) {
            return;
        }

        GlideRequest<Drawable> request = GlideApp.with(options.context).load(options.source);

        if (!options.memoryCache) {
            request.skipMemoryCache(true);
        }

        if (!options.diskCache) {
            request.diskCacheStrategy(DiskCacheStrategy.NONE);
        }

        if (options.loadListener != null) {
            request.listener(new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    return options.loadListener.onLoadFailed();
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    return options.loadListener.onLoadSuccess(resource);
                }
            });
        } else {
            request.listener(options.requestListener);
        }

        switch (options.transform) {
            case TRANSFORM_TYPE_DEFAULT:
            case TRANSFORM_CENTER_INSIDE:
                break;
            case TRANSFORM_CENTER_CROP:
                request.centerCrop();
                break;
            case TRANSFORM_FIT_CENTER:
                request.fitCenter();
                break;
            case TRANSFORM_ROUNDED_CORNERS:
                request.transform(new RoundedCorners(options.roundingRadius));
                break;
            case TRANSFORM_CENTER_CROP_ROUNDED_CORNERS:
                request.transform(new MultiTransformation<Bitmap>(new CenterCrop(), new RoundedCorners(options.roundingRadius)));
                break;
            case TRANSFORM_CIRCLE_CROP:
                request.transform(new CircleCrop());
                break;
            case TRANSFORM_CENTER_CROP_CIRCLE_CROP:
                request.transform(new MultiTransformation<Bitmap>(new CenterCrop(), new CircleCrop()));
                break;
            case TRANSFORM_CUSTOM:
                if (options.custom == null) {
                    break;
                }
                request.transform(options.custom);
                break;
        }
        if (options.imageView != null) {
            if (options.pre) {
                request.preload(options.imageView.getWidth(), options.imageView.getHeight());
                return;

            }
            request.into(options.imageView);
            return;
        }

        if (options.pre) {
            request.preload();
        }
    }

    public static class Options {
        private Context context;
        private Object source;
        private ImageView imageView;
        private int transform;
        private int roundingRadius = 10;
        private MultiTransformation custom;
        private boolean memoryCache = true;
        private boolean diskCache = true;
        private boolean pre = false;
        private RequestListener requestListener;
        private LoadListener loadListener;

        public Options(Context context) {
            this.context = context;
        }

        public Options source(Object source) {
            this.source = source;
            return this;
        }

        public Options transform(int transform) {
            this.transform = transform;
            return this;
        }

        public Options radiusDp(int radiusDp) {
            int radiusPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, radiusDp, Resources.getSystem().getDisplayMetrics());

            return radiusPx(radiusPx);
        }

        public Options radiusPx(int radiusPx) {
            this.roundingRadius = radiusPx;
            return this;
        }

        public Options custom(MultiTransformation custom) {
            this.custom = custom;
            return this;
        }

        public Options target(ImageView imageView) {
            this.imageView = imageView;
            return this;
        }

        public Options memoryCache(boolean cache) {
            this.memoryCache = cache;
            return this;
        }

        public Options diskCache(boolean cache) {
            this.diskCache = cache;
            return this;
        }

        public Options listener(RequestListener listener) {
            this.requestListener = listener;
            return this;
        }

        public Options listener(LoadListener listener) {
            this.loadListener = listener;
            return this;
        }

        public Options pre(boolean pre) {
            this.pre = pre;
            return this;
        }

        public ImageView getImageView() {
            return imageView;
        }

        public Object getSource() {
            return source;
        }

        public int getTransform() {
            return transform;
        }

        public int getRoundingRadius() {
            return roundingRadius;
        }
    }

    public interface LoadListener {
        public boolean onLoadSuccess(Drawable resource);

        public boolean onLoadFailed();
    }
}
